package com.example.rlw.mvpplugintest.home;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.rlw.mvpplugintest.R;
import com.example.rlw.mvpplugintest.adapter.HomeAdapter;
import com.example.rlw.mvpplugintest.bean.UserBean;
import com.example.rlw.mvpplugintest.details.DetailsActivity;
import com.example.rlw.mvpplugintest.mvp.MVPBaseActivity;
import com.example.rlw.mvpplugintest.prompt.EmptyCallback;
import com.example.rlw.mvpplugintest.prompt.LoadingCallback;
import com.kingja.loadsir.core.LoadSir;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class HomeActivity extends MVPBaseActivity<HomeContract.View, HomePresenter> implements HomeContract.View, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.tv_title_title)
    TextView mTvTitleTitle;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout mSwipeRefresh;

    private HomeAdapter mAdapter;
    private List<UserBean> mList;
    private int page = 0; //当前页面

    @Override
    protected int getLayoutId() {
        return R.layout.home_layout;
    }

    @Override
    protected void initView() {
        mTvTitleTitle.setText("列表页面");
    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        mLoadService = LoadSir.getDefault().register(mRecyclerView, this);
        mList = new ArrayList<>();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mAdapter = new HomeAdapter(R.layout.home_item_layout, mList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(this, mRecyclerView);
        mSwipeRefresh.setOnRefreshListener(this);
        mSwipeRefresh.setColorSchemeResources(new int[]{R.color.colorPrimary, R.color.colorPrimaryDark});
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initNet() {
        showLoadingView();
        getData();
    }

    @OnClick(R.id.ll_title_back)
    public void onViewClicked() {
        finish();
    }

    private int totalSize = 35;

    @Override
    public void onSuccess(List<UserBean> userBeans) {
        mSwipeRefresh.setRefreshing(false);
        if (page == 0) {
            mList.clear();
        }
        if (userBeans != null && page == 0 && userBeans.size() == 0) {
            mLoadService.showCallback(EmptyCallback.class);
        } else {
            mList.addAll(userBeans);
            mAdapter.setNewData(mList);
            if (mList.size() >= totalSize) {
                //加载完所有数据
                mAdapter.loadMoreEnd();
            } else {
                mAdapter.setEnableLoadMore(true);
                mAdapter.loadMoreComplete();
            }
        }
    }

    //上拉加载更多
    @Override
    public void onLoadMoreRequested() {
        page++;
        mAdapter.setEnableLoadMore(false);
        getData();
    }

    @Override
    public void onReload(View v) {
        super.onReload(v);
        mLoadService.showCallback(LoadingCallback.class);
        getData();
    }

    @Override
    public void onRefresh() {
        page = 0;
        getData();
    }

    public void getData() {
        mPresenter.getData("任利稳", "123456");
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        openActivity(DetailsActivity.class);
    }
}
